package pl.freetimetool.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import pl.freetimetool.common.AccountRoles;
import pl.freetimetool.domain.AccountRole;
import pl.freetimetool.domain.Worker;
import pl.freetimetool.exceptions.UserExistsException;
import pl.freetimetool.service.AccountService;

@Controller
public class WorkerController {

	@Autowired
	AccountService accountService;

	@RequestMapping(value = "/worker/delete", method = RequestMethod.GET)
	public ModelAndView delete() {
		final ModelAndView mav = new ModelAndView("/worker/delete");
		mav.addObject("worker", new Worker());
		return mav;
	}

	@RequestMapping(value = "/worker/delete", method = RequestMethod.POST)
	public ModelAndView delete(@Valid final Worker worker) {
		final ModelAndView mav = new ModelAndView("worker/delete");

		this.accountService.removeAccount(worker.getUsername());

		mav.setViewName("redirect:/worker/deleteConfirmed");
		return mav;
	}

	@RequestMapping(value = "/worker/deleteConfirmed", method = RequestMethod.GET)
	public ModelAndView deletionDetails() {
		final ModelAndView mav = new ModelAndView("common/deleteConfirmed");
		mav.addObject("acc", "Worker");
		return mav;
	}

	@RequestMapping(value = "/worker/add", method = RequestMethod.GET)
	public ModelAndView add() {
		final ModelAndView mav = new ModelAndView("/worker/add");
		mav.addObject("worker", new Worker());
		return mav;
	}

	@RequestMapping(value = "/worker/add", method = RequestMethod.POST)
	public ModelAndView add(@Valid final Worker worker, final BindingResult result) {
		final ModelAndView mav = new ModelAndView("worker/add");
		if (!worker.getPassword().equals(worker.getRepeatedPassword())) {
			result.addError(new FieldError("worker", "repeatedPassword", "notEqual"));
		}
		if (result.hasErrors()) {
			mav.addObject("worker", worker);
			return mav;
		}

		final List<AccountRole> ar = new ArrayList<AccountRole>();
		ar.add(new AccountRole(AccountRoles.WORKER));

		try {
			this.accountService.registerAccount(worker, ar);
		} catch (final UserExistsException e) {
		}
		mav.setViewName("redirect:/worker/showDetails/" + worker.getUsername());
		return mav;
	}

	@RequestMapping(value = "/worker/showDetails/{username}", method = RequestMethod.GET)
	public ModelAndView details(@PathVariable("username") final String username) {
		final ModelAndView mav = new ModelAndView("worker/showDetails");
		mav.addObject("worker", this.accountService.getAccount(username));
		return mav;
	}

	@RequestMapping(value = "/worker/workerPanel", method = RequestMethod.GET)
	public ModelAndView workerPanel() {
		final ModelAndView mav = new ModelAndView("worker/workerPanel");
		return mav;
	}

	@RequestMapping(value = "/worker/showWorkers", method = RequestMethod.GET)
	public ModelAndView showAll() {
		final ModelAndView mav = new ModelAndView("worker/getAll");
		final ArrayList<AccountRole> roles = new ArrayList<AccountRole>();
		roles.add(new AccountRole(AccountRoles.WORKER));
		mav.addObject("workers", this.accountService.listAccounts(roles));
		return mav;
	}
}
